package com.treasure.hunt.main

import android.app.Activity
import android.content.Intent
import android.text.Editable
import android.view.View
import android.view.inputmethod.EditorInfo
import com.blankj.utilcode.util.FragmentUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPStaticUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.module.lemlin.base.BaseAbstractActivity
import com.treasure.hunt.R
import com.treasure.hunt.goods.GoodsHomeFragment
import kotlinx.android.synthetic.main.activity_search.*

class MainSearchActivity : BaseAbstractActivity() {

    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, MainSearchActivity::class.java)
            activity.startActivity(intent)
        }
    }

    private val tagAdapter =
        object : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_search_record) {
            init {
                addChildClickViewIds(R.id.tvSearchTag)
            }
            override fun convert(holder: BaseViewHolder, item: String) {
                holder.setText(R.id.tvSearchTag, item)
            }
        }

    override val layoutResId: Int
        get() = R.layout.activity_search

    override fun stateBar(): StatusBar {
        return StatusBar(
            isLightMode = false,
            bottomColor = R.color.FF_020027,
            view = flSearchBar
        )
    }

    override fun initView() {
        ivSearchBack.setOnClickListener {
            onBackPressed()
        }

        rvSearchRecordBody.apply {
            val layoutManager: FlexboxLayoutManager =
                FlexboxLayoutManager(this@MainSearchActivity, FlexDirection.ROW, FlexWrap.WRAP)
            this.layoutManager = layoutManager
        }.adapter = tagAdapter.apply {
            setNewInstance(
                SPStaticUtils.getStringSet(
                    "searchRecord", mutableSetOf()
                ).toMutableList()
            )
        }

        tagAdapter.setOnItemChildClickListener{ adapter, view, position ->
            val record:String = tagAdapter.getItem(position)
            edSearchContent.text = Editable.Factory.getInstance().newEditable(record)
            flSearchBody.visibility = View.VISIBLE
            FragmentUtils.replace(
                supportFragmentManager,
                GoodsHomeFragment.newInstance(record),
                R.id.flSearchBody
            )
        }


        ivSearchRecordDelete.setOnClickListener {
            SPStaticUtils.remove("searchRecord")
            tagAdapter.setNewInstance(mutableListOf())
        }

        edSearchContent.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                val content: String = edSearchContent.text.toString()
                if (content.isEmpty()) {
                    return@setOnEditorActionListener true
                }

                val record: Set<String> = SPStaticUtils.getStringSet(
                    "searchRecord", mutableSetOf()
                ).toMutableList().apply { add(content) }.toSet()
                SPStaticUtils.put(
                    "searchRecord", record
                )
                tagAdapter.setNewInstance(record.toMutableList())
                flSearchBody.visibility = View.VISIBLE
                FragmentUtils.replace(
                    supportFragmentManager,
                    GoodsHomeFragment.newInstance(content),
                    R.id.flSearchBody
                )
                return@setOnEditorActionListener true
            }
            return@setOnEditorActionListener false
        }
    }

    override fun onBackPressed() {
        if (flSearchBody.visibility == View.VISIBLE) {
            flSearchBody.visibility = View.INVISIBLE
        } else {
            super.onBackPressed()
        }
    }
}